﻿using BaseDal;
using Common;
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseBll
{
    public class AssetUpkeepBll
    {
        private static readonly Lazy<AssetUpkeepBll> dal = new Lazy<AssetUpkeepBll>(() => new AssetUpkeepBll());
        public static AssetUpkeepBll Instance { get; } = dal.Value;
        // 获取单例实例
        readonly AssetUpkeepDao DalInstance;
        private AssetUpkeepBll()
        {
            DalInstance = AssetUpkeepDao.Instance;
        }

        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public bool IsExist(string strWhere)
        {
            return DalInstance.IsExist(strWhere);
        }

        /// <summary>
        /// 查询分页数据
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strWhere"></param>
        /// <param name="filedOrder"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public DataTable GetPageList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return DalInstance.GetPageList(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }

        /// <summary>
        /// 根据id查询实体类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AssetUpkeepHead GetModelById(int id)
        {
            return DalInstance.GetModelById(id);
        }

        /// <summary>
        /// 查询子表
        /// </summary>
        /// <param name="headID"></param>
        /// <returns></returns>
        public List<AssetUpkeepBody> GetBodyList(decimal headID)
        {
            return DalInstance.GetBodyList(headID);
        }

        /// <summary>
        /// 查询维护资产（新增、修改用）
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataTable GetKeepAsset(string strWhere)
        {
            return DalInstance.GetKeepAsset(strWhere);
        }

        /// <summary>
        /// 查看详情
        /// </summary>
        /// <param name="id"></param>
        /// <param name="assetNo"></param>
        /// <returns></returns>
        public DataTable GetDetail(int id, string assetNo)
        {
            return DalInstance.GetDetail(id, assetNo);
        }

        /// <summary>
        /// 通过headID,assetNo 查询维护登记子表数据
        /// </summary>
        /// <param name="headID"></param>
        /// <param name="assetNo"></param>
        /// <returns></returns>
        public AssetUpkeepBody GetAssetUpkeepBody(decimal headID, string assetNo)
        {
            return DalInstance.GetAssetUpkeepBody(headID, assetNo);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool AddAssetUpkeep(AssetUpkeepHead model)
        {
            return DalInstance.AddAssetUpkeep(model);
        }

        /// <summary>
        /// 撤销
        /// </summary>
        public bool Revoke(decimal ID, string assetNo,out string msg)
        {
            msg = "";
            //先删除子表数据
            int delBody = DalInstance.DeleteBody(ID, assetNo);

            bool result;
            if (delBody > 0)
            {
                msg = "撤销成功";
                //检查子表是否还有数据
                bool checkBody = DalInstance.CheckBody(ID);
                // 如果子表没用数据了，删除主表同时删除文件
                if (!checkBody)
                {
                    int delMain = DalInstance.DeleteHead(ID);
                    if (delMain > 0)
                    {
                        // 删除文件
                        var dt = UploadBll.Instance.GetFiles(ID, "AssetUpkeep");
                        if (dt.Rows.Count > 0)
                        {
                            var files = CommonOperation.ConvertDataTableToModelList<FileData>(dt);
                            var fid = "";
                            for (int i = 0; i < files.Count; i++)
                            {
                                if (string.IsNullOrEmpty(fid))
                                {
                                    fid = "'" + files[i].RelationId + "'";
                                }
                                else
                                {
                                    fid += ",'" + files[i].RelationId + "'";
                                }
                            }
                            var res = UploadBll.Instance.DeleteFiles(fid, "AssetUpkeep", files);
                            if (!res)
                            {
                                msg = "文件删除失败";
                            }
                        }
                    }
                    else
                    {
                        msg = "主表删除失败";

                    }
                }
                result = true;
            }
            else
            {
                result = false;
            }

            return result;
           
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int UpdateStatus(decimal id, int status)
        {
            return DalInstance.UpdateStatus(id, status);
        }

        /// <summary>
        /// 修改子表状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int UpdateStatusTag(decimal id, int status, string userName)
        {
            return DalInstance.UpdateStatusTag(id, status, userName);
        }

        /// <summary>
        /// 验证model各字段合法性
        /// </summary>
        /// <param name="model"></param>
        /// <param name="msg">错误信息</param>
        /// <returns></returns>
        public bool VerifyModel(AssetUpkeepHead model, ref string msg)
        {
            if (string.IsNullOrEmpty(model.AU_Number))
            {
                msg = "请填写维护编号";
                return false;
            }
            if (model.AU_Date == DateTime.MinValue)
            {
                msg = "请选择维护时间";
                return false;
            }

            return true;
        }

    }
}
